﻿using DocumentFormat.OpenXml.Lite.Core;
using DocumentFormat.OpenXml.Lite.Helper;
using DocumentFormat.OpenXml.Lite.Shared;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Table;
using DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Types;
using DocumentFormat.OpenXml.Lite.Xml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

namespace DocumentFormat.OpenXml.Lite.WordprocessingML.Word.Style
{
    [XmlRoot("style", Namespace = Constants.WordMain)]
    public class StyleElement : XmlSerializable
    {
        #region Attributes

        [Optional]
        [XmlAttribute("type", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_StyleType> StyleType { get; set; }

        [Optional]
        [XmlAttribute("styleId", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public string StyleId { get; set; }

        [Optional]
        [XmlAttribute("default", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_OnOff> DefaultStyle { get; set; }

        [Optional]
        [XmlAttribute("customStyle", Namespace = Constants.WordMain, Form = XmlSchemaForm.Qualified)]
        public Nullable<ST_OnOff> CustomStyle { get; set; }

        #endregion

        #region Elements

        [XmlElement("", Namespace = Constants.WordMain)]
        public XmlList<IStyleElement> StyleElements { get; set; }

        [XmlElement("rPr", Namespace = Constants.WordMain)]
        public Base.RunProperties RunProperties { get; set; }

        [XmlElement("pPr", Namespace = Constants.WordMain)]
        public Base.ParagraphProperties ParagraphProperties { get; set; }

        [XmlElement("tblPr", Namespace = Constants.WordMain)]
        public TableProperties TableProperties { get; set; }

        [XmlElement("trPr", Namespace = Constants.WordMain)]
        public TableRowProperties TableRowProperties { get; set; }

        [XmlElement("tcPr", Namespace = Constants.WordMain)]
        public TableCellProperties TableCellProperties { get; set; }

        [XmlElement("tblStylePr", Namespace = Constants.WordMain)]
        public XmlList<TableStyleProperties> TableStyleElements { get; set; }

        #endregion

        public StyleElement()
        {
            TableStyleElements = new XmlList<TableStyleProperties>();

            StyleElements = new XmlList<IStyleElement>();
            StyleElements.Changing += StyleElements_Changing;
        }

        #region Methods

        public TableStyleProperties AddTableStyle()
        {
            TableStyleProperties p = new TableStyleProperties();
            TableStyleElements.Add(p);
            return p;
        }

        /// <summary>
        /// Hint: you cannot add two elements with the same type.
        /// </summary>
        /// <param name="element"></param>
        /// <returns>True, if the list not contains element with the same type. 
        /// False otherwise (in this case the content will be override the old one).</returns>
        public bool AddStyleElement(IStyleElement element)
        {
            var obj = StyleElements.FirstOrDefault(e => e.GetType() == element.GetType());
            if (obj != null)
            {
                (obj as IStyleElement).Copy(element);
            }
            else
            {
                StyleElements.Add(element);
                return true;
            }

            return false;
        }

        #endregion

        #region Events

        void StyleElements_Changing(object sender, ListEventArgs<IStyleElement> args)
        {
            if (args.Type == ListEventType.Remove)
                return;

            foreach (var obj in args.Objects)
            {
                if (obj.GetType().IsInheritedFrom(typeof(IStyleElement)))
                {
                    var element = StyleElements.FirstOrDefault(e => e.GetType() == obj.GetType());
                    if (obj != null)
                    {
                        throw new OpenXmlException("Element with the same type (" +
                            element.GetType().ToString() + ") already added to list: " + StyleElements.ToString());
                    }
                }
            }
        }

        #endregion
    }
}
